﻿#region Copyright (c) 2007-2011 SmartVault Corporation, all rights reserved.
// 
// http://www.opensource.org/licenses/mit-license.php
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// 
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using Interop.SVault.Client.SDK;
using Interop.SVault.Client.SDK.Utils;

namespace CRMStore
{
    [Serializable()]
    public class CRMDatabase
    {
        private string _qbDatabasePath;
        private string _qbIdentifier;
        private string _qbAppType;
        private BindingList<Customer> _customers = new BindingList<Customer>();
        private List<string> _homeSync = new List<string>();
        private Dictionary<string, string> _homeSyncLookup = new Dictionary<string, string>();

        private const string OurClientId = "SampleCRMWinClient";
        private const string OurApplicationId = "SampleCRM";
        private const string OurApplicationInstanceId = "{20EE5F10-A808-462e-B50A-75D4D4E1605E}";
        private const string OurApplicationName = "SmartVault CRM Store Sample";

        private ICustomToolbar _toolbar;
        private IApplicationContext _toolbarContext;

        public CRMDatabase()
        {
        }

        public static  ISmartVaultClientUtils CreateClientUtils()
        {
            return Activator.CreateInstance(Type.GetTypeFromProgID("SmartVault.Client.Utils")) as ISmartVaultClientUtils;
        }

        public static ISmartVaultQBDataAccess CreateQBDataAccess(string companyPath)
        {
            return CreateClientUtils().CreateQBDataAccess(companyPath, OurApplicationName);
        }

        public static ISmartVaultClientSDK GetSDK()
        {
            ISmartVaultClientSDK sdk = Activator.CreateInstance(Type.GetTypeFromProgID("SmartVault.Client.SDK")) as ISmartVaultClientSDK;
            sdk.Unlock(OurClientId);
            return sdk;
        }

        public string InstanceName
        {
            get { return "SmartVault Example Application"; }
        }

        public IApplicationContext OpenToolbar(ICustomToolbar toolbar)
        {
            _toolbar = toolbar;
            _toolbar.Unlock(OurClientId);
            _toolbarContext = _toolbar.OpenToolbar(OurApplicationId, OurApplicationInstanceId, InstanceName);
            return _toolbarContext;
        }

        public IApplicationContext OpenQBContext(string companyPath)
        {
            ISmartVaultQBDataAccess access = CreateQBDataAccess(companyPath);
            ISmartVaultClientSDK sdk = GetSDK();
            return sdk.Open(access.ApplicationType, access.ApplicationId, InstanceName);
        }

        public IApplicationContext OpenQBContext()
        {
            if (string.IsNullOrEmpty(this.QBDatabasePath))
            {
                throw new InvalidOperationException();
            }

            return OpenQBContext(this.QBDatabasePath);
        }

        public IApplicationContext OpenContext()
        {
            return GetSDK().Open(OurApplicationId, OurApplicationInstanceId, InstanceName);
        }

        public void ProvisionWithQB(IApplicationContext context)
        {
            if (QBAppType == null || QBIdentifier == null)
                throw new InvalidOperationException();

            context.ProvisionAsSibling(QBAppType, QBIdentifier);
        }

        public IApplicationContext GetToolbarContext()
        {
            return _toolbarContext;
        }

        public string QBAppType
        {
            get
            {
                return _qbAppType;
            }
            set
            {
                _qbAppType = value;
            }
        }

        public string QBDatabasePath
        {
            get
            {
                return _qbDatabasePath;
            }
            set
            {
                _qbDatabasePath = value;
            }
        }

        public string QBIdentifier
        {
            get
            {
                return _qbIdentifier;
            }
            set
            {
                _qbIdentifier = value;
            }
        }

        public void RemoveCustomer(Guid g)
        {
            for (int i = 0; i < _customers.Count; ++i)
            {
                if (_customers[i].Id.Equals(g))
                {
                    _customers.RemoveAt(i);
                    return;
                }
            }
        }

        public List<string> HomeSync
        {
            get
            {
                return _homeSync;
            }
            set
            {
                _homeSync = value;

                _homeSyncLookup = new Dictionary<string, string>();

                foreach (string doc in _homeSync)
                {
                    _homeSyncLookup.Add(doc, doc);
                }
            }
        }

        public bool IsHomeSynced(string doc)
        {
            return _homeSyncLookup.ContainsKey(doc);
        }

        public void MarkSynced(string doc)
        {
            if (!_homeSyncLookup.ContainsKey(doc))
            {
                _homeSyncLookup.Add(doc, doc);
                _homeSync.Add(doc);
            }
        }

        public BindingList<Customer> Customers
        {
            get
            {
                return _customers;
            }
            set
            {
                _customers = value;

            }
        }
    }
}
